//
//  main.m
//  05_循环遍历
//
//  Created by 徐昊 on 16/7/8.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {

    // 定义一个数组, 包含5个字符串对象, 倒序取出数组中的所有元素, 并存储到另一个可变数组中.
    NSArray *stringArr = @[@"于瀚", @"韩旭", @"李成", @"报名李", @"杜江"];
    NSMutableArray *newStringArr = [NSMutableArray array];
    for (NSInteger i = stringArr.count - 1; i >= 0 ; i--) {
        [newStringArr addObject:stringArr[i]];
    }
    NSLog(@"%@", newStringArr);
    
    // 定义一个字典, 用于存储一个人的name, sex, age三个数据, 完成对其三个数据的输出
    NSDictionary *person = @{@"name": @"张三",
                              @"age": @"18",
                              @"sex": @"男"};
    NSArray *arr = person.allKeys;
    for (int i = 0; i < arr.count; i++) {
        NSString *str = [person objectForKey:arr[i]];
        NSLog(@"%@", str);
    }
    
    // 枚举器
    // NSEnumerator依赖于集合类型(数组, 字典, 集合), 不能单独使用
    NSEnumerator *stringArrayEnumerator = [stringArr objectEnumerator];
    id value = nil;
    while ((value = [stringArrayEnumerator nextObject]) != nil) {
        NSLog(@"%@", value);
    }
    
    // 倒序枚举
    NSEnumerator *reverse = [stringArr reverseObjectEnumerator];
    id value1 = nil;
    while (value1 = [reverse nextObject]) {
        NSLog(@"value1 = %@", value1);
    }
    
    // 字典枚举器(字典是无序的, 所以没有倒序)
    // 默认取出来的值, 是value(想取出key, 可以使用allKeys)
    NSEnumerator *dicEnumerator = [person objectEnumerator];
    id value2 = nil;
    while (value2 = [dicEnumerator nextObject]) {
        NSArray *keys = [person allKeysForObject:value2];
        NSLog(@"value2 = %@, keys = %@", value2, keys);
    }
    
    // 集合枚举(无序, 没有倒序枚举)
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"114", @"119", @"213312", @"123213", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"setValue = %@", setValue);
    }
    
    // 快速枚举 for ... in
    // 定义一个数组, 包含5个字符串对象, 取出数组中所有的字符串, 并拼接一个生成一个新的字符串
    for (NSString *string in stringArr) {
        NSLog(@"%@", string);
    }
    
    NSMutableString *appendString = [NSMutableString string];
    for (NSString *str in stringArr) {
        [appendString appendString:str];
    }
    NSLog(@"%@", appendString);
    
    // 快速枚举一个字典
    for (NSString *dicValue in person) {
        NSLog(@"%@", dicValue);
        NSLog(@"value = %@", [person valueForKey:dicValue]);
    }
    // 快速枚举一个集合
    for (NSString *anyObject in set) {
        NSLog(@"antObject = %@", anyObject);
    }
    
    // 排序
    // 参数1 Key: 排序条件
    // 参数2 ascending: YES(升序) or NO(降序)
    NSMutableArray *mArr = [NSMutableArray arrayWithObjects:@10, @13, @15, @23, nil];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
    // 数组有排序的方法(sortedArrayUsingDescriptors)
    // 参数为一个排序条件的数组
    stringArr = [stringArr sortedArrayUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", stringArr);
    [mArr sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", mArr);
    // 排序: 可变数组和不可变数组通过sort排序调用的方法不同
    // 1.不可变数组, 排序后需要生成新的数组
    // 2.可变数组, 排序直接可以在本身之上修改
    
    Person *per1 = [[Person alloc] init];
    per1.name = @"x";
    per1.age = 20;
    Person *per2 = [[Person alloc] init];
    per2.name = @"xx";
    per2.age = 23;
    Person *per3 = [[Person alloc] init];
    per3.name = @"xxxxxx";
    per3.age = 45;
    Person *per4 = [[Person alloc] init];
    per4.name = @"xxxx";
    per4.age = 46;
    Person *per5 = [[Person alloc] init];
    per5.name = @"xxxxx";
    per5.age = 12;
    NSArray *perArray = @[per1, per2, per3, per4, per5];
    NSLog(@"排序前: %@", perArray);
    // 根据name排序
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    perArray = [perArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"排序后: %@", perArray);
    
    // 根据age排序
    NSSortDescriptor *perSort2 = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    perArray = [perArray sortedArrayUsingDescriptors:@[perSort2]];
    
    // 排序2  
    NSArray *sortString = @[@"iPhone", @"iPad", @"iTouch", @"iMac", @"Mac Mini"];
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);
    
    
    
    return 0;
}
